<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/customwidgetplugin.qdoc -->
<head>
  <title>Qt 4.3: Custom Widget Plugin Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Custom Widget Plugin Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="designer-customwidgetplugin-analogclock-cpp.html">designer/customwidgetplugin/analogclock.cpp</a></li>
<li><a href="designer-customwidgetplugin-analogclock-h.html">designer/customwidgetplugin/analogclock.h</a></li>
<li><a href="designer-customwidgetplugin-customwidgetplugin-cpp.html">designer/customwidgetplugin/customwidgetplugin.cpp</a></li>
<li><a href="designer-customwidgetplugin-customwidgetplugin-h.html">designer/customwidgetplugin/customwidgetplugin.h</a></li>
</ul>
<p>The Custom Widget example shows how to create a custom widget plugin for <i>Qt Designer</i>.</p>
<p align="center"><img src="images/customwidgetplugin-example.png" /></p><p>In this example, the custom widget used is based on the <a href="widgets-analogclock.html">Analog Clock example</a>, and does not provide any custom signals or slots.</p>
<a name="preparation"></a>
<h2>Preparation</h2>
<p>To provide a custom widget that can be used with <i>Qt Designer</i>, we need to supply a self-contained implementation and provide a plugin interface. In this example, we reuse the <a href="widgets-analogclock.html">Analog Clock example</a> for convenience.</p>
<p>Since custom widgets plugins rely on components supplied with <i>Qt Designer</i>, the project file that we use needs to contain information about <i>Qt Designer</i>'s library components:</p>
<pre> TEMPLATE    = lib
 CONFIG      += designer plugin debug_and_release</pre>
<p>The <tt>TEMPLATE</tt> variable's value makes <tt>qmake</tt> create the custom widget as a library. Later, we will ensure that the widget will be recognized as a plugin by Qt by using the <a href="qtplugin.html#Q_EXPORT_PLUGIN2">Q_EXPORT_PLUGIN2</a>() macro to export the relevant widget information.</p>
<p>The <tt>CONFIG</tt> variable contains two values, <tt>designer</tt> and <tt>plugin</tt>:</p>
<ul>
<li><tt>designer</tt>: Since custom widgets plugins rely on components supplied with <i>Qt Designer</i>, this value ensures that our plugin links against <i>Qt Designer</i>'s library (<tt>libQtDesigner.so</tt>).</li>
<li><tt>plugin</tt>: We also need to ensure that <tt>qmake</tt> considers the custom widget a plugin library.</li>
</ul>
<p>When Qt is configured to build in both debug and release modes, <i>Qt Designer</i> will be built in release mode. When this occurs, it is necessary to ensure that plugins are also built in release mode. For that reason we add the <tt>debug_and_release</tt> value to the <tt>CONFIG</tt> variable. Otherwise, if a plugin is built in a mode that is incompatible with <i>Qt Designer</i>, it won't be loaded and installed.</p>
<p>The header and source files for the widget are declared in the usual way, and we provide an implementation of the plugin interface so that <i>Qt Designer</i> can use the custom widget:</p>
<pre> HEADERS     = analogclock.h \
               customwidgetplugin.h
 SOURCES     = analogclock.cpp \
               customwidgetplugin.cpp</pre>
<p>It is also important to ensure that the plugin is installed in a location that is searched by <i>Qt Designer</i>. We do this by specifying a target path for the project and adding it to the list of items to install:</p>
<pre> target.path = $$[QT_INSTALL_PLUGINS]/designer
 INSTALLS += target</pre>
<p>The custom widget is created as a library, and will be installed alongside the other <i>Qt Designer</i> plugins when the project is installed (using <tt>make install</tt> or an equivalent installation procedure). Later, we will ensure that it is recognized as a plugin by <i>Qt Designer</i> by using the <a href="qtplugin.html#Q_EXPORT_PLUGIN2">Q_EXPORT_PLUGIN2</a>() macro to export the relevant widget information.</p>
<p>Note that if you want the plugins to appear in a Visual Studio integration, the plugins must be built in release mode and their libraries must be copied into the plugin directory in the install path of the integration (for an example, see <tt>C:/program files/trolltech as/visual studio integration/plugins</tt>).</p>
<p>For more information about plugins, see the <a href="plugins-howto.html">How to Create Qt Plugins</a> documentation.</p>
<a name="analogclock-class-definition-and-implementation"></a>
<h2>AnalogClock Class Definition and Implementation</h2>
<p>The <tt>AnalogClock</tt> class is defined and implemented in exactly the same way as described in the <a href="widgets-analogclock.html">Analog Clock example</a>. Since the class is self-contained, and does not require any external configuration, it can be used without modification as a custom widget in <i>Qt Designer</i>.</p>
<a name="analogclockplugin-class-definition"></a>
<h2>AnalogClockPlugin Class Definition</h2>
<p>The <tt>AnalogClock</tt> class is exposed to <i>Qt Designer</i> through the <tt>AnalogClockPlugin</tt> class. This class inherits from both <a href="qobject.html">QObject</a> and the <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a> class, and implements an interface defined by <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a>:</p>
<pre> class AnalogClockPlugin : public QObject, public QDesignerCustomWidgetInterface
 {
     Q_OBJECT
     Q_INTERFACES(QDesignerCustomWidgetInterface)

 public:
     AnalogClockPlugin(QObject *parent = 0);

     bool isContainer() const;
     bool isInitialized() const;
     QIcon icon() const;
     QString domXml() const;
     QString group() const;
     QString includeFile() const;
     QString name() const;
     QString toolTip() const;
     QString whatsThis() const;
     QWidget *createWidget(QWidget *parent);
     void initialize(QDesignerFormEditorInterface *core);

 private:
     bool initialized;
 };</pre>
<p>The functions provide information about the widget that <i>Qt Designer</i> can use in the <a href="designer-getting-started.html#widgetbox">widget box</a>. The <tt>initialized</tt> private member variable is used to record whether the plugin has been initialized by <i>Qt Designer</i>.</p>
<p>Note that the only part of the class definition that is specific to this particular custom widget is the class name.</p>
<a name="analogclockplugin-implementation"></a>
<h2>AnalogClockPlugin Implementation</h2>
<p>The class constructor simply calls the <a href="qobject.html">QObject</a> base class constructor and sets the <tt>initialized</tt> variable to <tt>false</tt>.</p>
<pre> AnalogClockPlugin::AnalogClockPlugin(QObject *parent)
     : QObject(parent)
 {
     initialized = false;
 }</pre>
<p><i>Qt Designer</i> will initialize the plugin when it is required by calling the <tt>initialize()</tt> function:</p>
<pre> void AnalogClockPlugin::initialize(QDesignerFormEditorInterface * <span class="comment">/* core */</span>)
 {
     if (initialized)
         return;

     initialized = true;
 }</pre>
<p>In this example, the <tt>initialized</tt> private variable is tested, and only set to <tt>true</tt> if the plugin is not already initialized. Although, this plugin does not require any special code to be executed when it is initialized, we could include such code after the test for initialization.</p>
<p>The <tt>isInitialized()</tt> function lets <i>Qt Designer</i> know whether the plugin is ready for use:</p>
<pre> bool AnalogClockPlugin::isInitialized() const
 {
     return initialized;
 }</pre>
<p>Instances of the custom widget are supplied by the <tt>createWidget()</tt> function. The implementation for the analog clock is straightforward:</p>
<pre> QWidget *AnalogClockPlugin::createWidget(QWidget *parent)
 {
     return new AnalogClock(parent);
 }</pre>
<p>In this case, the custom widget only requires a <tt>parent</tt> to be specified. If other arguments need to be supplied to the widget, they can be introduced here.</p>
<p>The following functions provide information for <i>Qt Designer</i> to use to represent the widget in the widget box. The <tt>name()</tt> function returns the name of class that provides the custom widget:</p>
<pre> QString AnalogClockPlugin::name() const
 {
     return &quot;AnalogClock&quot;;
 }</pre>
<p>The <tt>group()</tt> function is used to describe the type of widget that the custom widget belongs to:</p>
<pre> QString AnalogClockPlugin::group() const
 {
     return &quot;Display Widgets [Examples]&quot;;
 }</pre>
<p>The widget plugin will be placed in a section identified by its group name in <i>Qt Designer</i>'s widget box. The icon used to represent the widget in the widget box is returned by the <tt>icon()</tt> function:</p>
<pre> QIcon AnalogClockPlugin::icon() const
 {
     return QIcon();
 }</pre>
<p>In this case, we return a null icon to indicate that we have no icon that can be used to represent the widget.</p>
<p>A tool tip and &quot;What's This?&quot; help can be supplied for the custom widget's entry in the widget box. The <tt>toolTip()</tt> function should return a short message describing the widget:</p>
<pre> QString AnalogClockPlugin::toolTip() const
 {
     return &quot;&quot;;
 }</pre>
<p>The <tt>whatsThis()</tt> function can return a longer description:</p>
<pre> QString AnalogClockPlugin::whatsThis() const
 {
     return &quot;&quot;;
 }</pre>
<p>The <tt>isContainer()</tt> function tells <i>Qt Designer</i> whether the widget is supposed to be used as a container for other widgets. If not, <i>Qt Designer</i> will not allow the user to place widgets inside it.</p>
<pre> bool AnalogClockPlugin::isContainer() const
 {
     return false;
 }</pre>
<p>Most widgets in Qt can contain child widgets, but it only makes sense to use dedicated container widgets for this purpose in <i>Qt Designer</i>. By returning <tt>false</tt>, we indicate that the custom widget cannot hold other widgets; if we returned true, <i>Qt Designer</i> would allow other widgets to be placed inside the analog clock and a layout to be defined.</p>
<p>The <tt>domXml()</tt> function provides a way to include default settings for the widget in the standard XML format used by <i>Qt Designer</i>. In this case, we only specify the widget's geometry:</p>
<pre> QString AnalogClockPlugin::domXml() const
 {
     return &quot;&lt;widget class=\&quot;AnalogClock\&quot; name=\&quot;analogClock\&quot;&gt;\n&quot;
            &quot; &lt;property name=\&quot;geometry\&quot;&gt;\n&quot;
            &quot;  &lt;rect&gt;\n&quot;
            &quot;   &lt;x&gt;0&lt;/x&gt;\n&quot;
            &quot;   &lt;y&gt;0&lt;/y&gt;\n&quot;
            &quot;   &lt;width&gt;100&lt;/width&gt;\n&quot;
            &quot;   &lt;height&gt;100&lt;/height&gt;\n&quot;
            &quot;  &lt;/rect&gt;\n&quot;
            &quot; &lt;/property&gt;\n&quot;
            &quot; &lt;property name=\&quot;toolTip\&quot; &gt;\n&quot;
            &quot;  &lt;string&gt;The current time&lt;/string&gt;\n&quot;
            &quot; &lt;/property&gt;\n&quot;
            &quot; &lt;property name=\&quot;whatsThis\&quot; &gt;\n&quot;
            &quot;  &lt;string&gt;The analog clock widget displays &quot;
            &quot;the current time.&lt;/string&gt;\n&quot;
            &quot; &lt;/property&gt;\n&quot;
            &quot;&lt;/widget&gt;\n&quot;;
 }</pre>
<p>If the widget provides a reasonable size hint, it is not necessary to define it here. In addition, returning an empty string instead of a <tt>&lt;widget&gt;</tt> element will tell <i>Qt Designer</i> not to install the widget in the widget box.</p>
<p>To make the analog clock widget usable by applications, we implement the <tt>includeFile()</tt> function to return the name of the header file containing the custom widget class definition:</p>
<pre> QString AnalogClockPlugin::includeFile() const
 {
     return &quot;analogclock.h&quot;;
 }</pre>
<p>Finally, we use the <a href="qtplugin.html#Q_EXPORT_PLUGIN2">Q_EXPORT_PLUGIN2</a>() macro to export the <tt>AnalogClockPlugin</tt> class for use with <i>Qt Designer</i>:</p>
<pre> Q_EXPORT_PLUGIN2(customwidgetplugin, AnalogClockPlugin)</pre>
<p>This macro ensures that <i>Qt Designer</i> can access and construct the custom widget. Without this macro, there is no way for <i>Qt Designer</i> to use the widget.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
